home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 2136 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  2.0 KB

  1. Path: usenet.eel.ufl.edu!pacifier!usenet
  2. From: dgager@pacifier.com (Dave Gager)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Help with cin.get()
  5. Date: 16 Jan 1996 04:28:31 GMT
  6. Organization: Pacifier Internet Server (360) 693-0325
  7. Message-ID: <4df9hf$fec@news.pacifier.com>
  8. References: <4cqbja$62n@earth.njcc.com>
  9. Reply-To: dgager@pacifier.com
  10. NNTP-Posting-Host: ip61.van2.pacifier.com
  11. Mime-Version: 1.0
  12. X-Newsreader: WinVN 0.99.2
  13.  
  14. In article <4cqbja$62n@earth.njcc.com>, chris@pluto.njcc.com says...
  15. >
  16. >I use this function to input a book title with imbedded blanks:
  17. >
  18. >    void getdata()
  19. >       {
  20. >       cout << "Enter title: ";
  21. >       cin.get(title, LEN);
  22. >       cout << "Enter price: ";
  23. >       cin >> price;
  24. >       }
  25. >
  26. >problem is, if I execute the function more than once, it skips the title 
  27. input 
  28. >on the second and every subsequent execution (i.e. the line will 
  29. resemble:
  30. >     Enter title: Enter price: 
  31. >never offering the oppurtunity to enter the title).
  32. >
  33. >I suspect this little "bug" arises from a subtle misuse of cin.get().  
  34. Can 
  35. >anyone identify it for me?  This is obviously a beginner question.
  36. >
  37. >My thanks and apologies.
  38. >chris.
  39. >chris@pluto.njcc.com
  40.  
  41.  
  42.  Two things come to mind without seeing the rest of the code....
  43.  
  44. Number one: Your variable LEN for the max number of chars in the book 
  45. title... if the title doesn't fill the the max number, your enter price 
  46. will fill the remaining buffer.
  47.  
  48. Number two: You need to flush out the input stream to remove any previous 
  49. residue, and to help alleviate the problem number one above. It's been a 
  50. while, but I think there is a clear() or flush() function to accomplish 
  51. this. Check your compilers language reference to find out.
  52.                                          Hope this helps!
  53.                                                    -Dave
  54.  
  55. -- 
  56. -------------------------------------------------------------------------
  57. -----
  58. Pacifier Online Data Service Dialup SLIP/PPP User
  59. To register: (360) 693-0325 or telnet pods.pacifier.com
  60. -------------------------------------------------------------------------
  61. -----
  62.  
  63.